package cn.virens.common.utils.common;

import cn.virens.common.exception.ExceptionUtil;

import java.util.Collection;

/**
 * Closeable 工具类，适用于各种流的关闭操作
 *
 * @author : virens
 */
public class CloseableUtil {

    /**
     * 关闭实现AutoCloseable的对象
     *
     * @param closeable AutoCloseable实现对象
     */
    public static void close(AutoCloseable closeable) {
        if (closeable == null) return;

        try {
            closeable.close();
        } catch (Exception e) {
            ExceptionUtil.log(e);
        }
    }

    /**
     * 关闭实现AutoCloseable的对象
     *
     * @param closeables AutoCloseable实现对象
     */
    public static void closes(Collection<? extends AutoCloseable> closeables) {
        if (closeables == null || closeables.isEmpty()) return;

        for (AutoCloseable closeable : closeables) {
            CloseableUtil.close(closeable);
        }
    }

    /**
     * 关闭实现AutoCloseable的对象
     *
     * @param closeables AutoCloseable实现对象
     */
    public static void closes(AutoCloseable... closeables) {
        for (AutoCloseable closeable : closeables) {
            CloseableUtil.close(closeable);
        }
    }
}
